/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.me.stepapp.persistence;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.widget.SimpleCursorAdapter;

/**
 *
 * @author Amber
 */
public class LocationProvider extends ContentProvider {
    View input_view;

    SimpleCursorAdapter  myAdapter;
    Cursor cursor;
    LocationDBAdapter db = null;

    public static final String PROVIDER_NAME = "org.me.stepapp.webapplocation";

    public static final Uri CONTENT_URI = Uri.parse("content://"+ PROVIDER_NAME + "/locations");

    private static final int LOCATIONS = 1;
    private static final int LOCATION_ID = 2;

    private static final UriMatcher uriMatcher;

    static{
        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        uriMatcher.addURI(PROVIDER_NAME, "locations", LOCATIONS);
        uriMatcher.addURI(PROVIDER_NAME, "locations/#", LOCATION_ID);
   }

      /*
       Observe from the code above that you use a UriMatcher object to parse the content URI that is passed to the content provider through a Content Resolver. For example, the following content URI represents a request for all locations in the content provider:

            content://net.learn2develop.provider.Locations/locations

        In contrast, the following represents a request for a particular location with _id=5:

            content://net.learn2develop.provider.MailingList/locations/5

       * */

    /* Action when onCreat is called */
    @Override
    public boolean onCreate() {
        Context context = getContext();
        db = new LocationDBAdapter(context);
        if ( db != null)  {
            db.open();
        }
        return (db == null)? false:true;
    }

    /* Query the content Uri using cursor */
    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        Cursor c=null;
        String qid=null;
        // if getting a specific location
        if (uriMatcher.match(uri) == LOCATION_ID){
            qid=""+ uri.getPathSegments().get(1);
        }
        c = db.query(qid, selection, selectionArgs, sortOrder);
        //  Registration watches content Uri for changes
        c.setNotificationUri(getContext().getContentResolver(), uri);
        return c;
   }

    /* get String at Uri */
    @Override
    public String getType(Uri uri) {
        switch (uriMatcher.match(uri)){
            // Get all locations
            case LOCATIONS:
                return "org.me.stepapp.cursor.dir/org.me.stepapp.webapplocation ";
            // Get a location
            case LOCATION_ID:
                return "org.me.stepapp.cursor.item/org.me.stepapp.webapplocation ";
            default:
                throw new IllegalArgumentException("Unsupported URI: " + uri);
        }
    }

    /* Insert content into Uri */
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        //---add a new location to Uri--//
        long rowID = db.insertLocation(values);
        //---if added successfully---
        if (rowID>0)  {
            Uri _uri = ContentUris.withAppendedId(CONTENT_URI, rowID);
            getContext().getContentResolver().notifyChange(_uri, null);
            return _uri;
        }
        throw new SQLException("Failed to insert row into " + uri);
   }

   /* Deletes Uri content */
    @Override
    public int delete(Uri arg0, String whereClause, String[] whereArgs) {
        // arg0 = uri
        // arg1 = selection
        // arg2 = selectionArgs
        long count=0;
        switch (uriMatcher.match(arg0)){
        case LOCATIONS:
            count= db.deleteLocations(whereClause, whereArgs);
            break;
        case LOCATION_ID:
            String id = arg0.getPathSegments().get(1);
            String whereClause1= Location.KEY_ROWID + " = " + id +
               (!TextUtils.isEmpty(whereClause) ? " AND (" +
               whereClause + ')' : "");
            count=db.deleteLocations(whereClause1, whereArgs);
            break;
        default:
            throw new IllegalArgumentException("Unknown URI " + arg0);
      }
      getContext().getContentResolver().notifyChange(arg0, null);
      return (int)count;
   }

   /*  Update content in Uri */
    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs)  {
        int count = 0;
        switch (uriMatcher.match(uri)){
            case LOCATIONS:
                count = db.update(values, selection, selectionArgs);
                break;
            case LOCATION_ID:
                String sel = Location.KEY_ROWID + " = " + uri.getPathSegments().get(1) +
                    (!TextUtils.isEmpty(selection) ? " AND (" +
                    selection + ')' : "");

             count = db.update(values, sel, selectionArgs);

            break;
         default: throw new IllegalArgumentException(
            "Unknown URI " + uri);
      }
      getContext().getContentResolver().notifyChange(uri, null);
      return count;
   }
}

